<?php

namespace app\admin\controller;

use my\Security;
use my\Spreadcode;
use think\Db;
use my\Tools;
use my\FTP;
use qrcode\QRcode;
class ProduceCode extends Base
{
    //获取二维码并存入数据库
    public function produce_code($driverId,$driverType)
    {
        Security::encrypt($driverId);//加密
        $update['type']=$driverType;
        $content = "https://h5.xiaomachuxing.cn/driver/recruit.html?driverId=".$driverId."&type=".$driverType;
        $path = ROOT_PATH.'/tmp/';
        $file = $driverId.'.png';
        $file_name = 'qrcode';
        $result = $this->makeQRCode($content,$path.'driver'.$driverId.$file,$file_name,'L','6','driver'.$driverId);

        if($result['status'] == 1){
            $update['base64']=$result['qr_code'];
            $update['errmsg']='';
        }else{
            $this->error('生成二维码失败', '', '', 1);
            $update['errmsg']='生成二维码失败';
        }

        $map='driver_id='.$driverId;
        $driver_qrcode_base64 = Db::name('driver_qrcode_base64')->where($map)->find();
        if($driver_qrcode_base64){
            Db::name('driver_qrcode_base64')->where($map)->update($update);
        }else{
            $update['driver_id']=$driverId;
            Db::name('driver_qrcode_base64')->insertGetId($update);
        }
        return $result['qr_code'];
    }
    public function produce_code1($driverId,$driverType)
    {
        $page='pages/index/index';
        $str='driver_id='.$driverId;
        $result=Spreadcode::mpcode($page,$str);
        $result1=Spreadcode::qrcodes($page,$str);
        $map='driver_id='.$driverId;
        $update['type']=$driverType;
        //{"errcode":47001,"errmsg":"data format error hint: [ehcDlo4FE-nZy.Ka] rid: 5f2a5b28-36d3ac4e-77094313"}

        if($result['errcode']>0){
            $update['errmsg']=$result;
        }else{
            $update['base64']=$result;
            $update['errmsg']='';
        }

        $driver_qrcode_base64 = Db::name('driver_qrcode_base64')->where($map)->find();
        if($driver_qrcode_base64){
            Db::name('driver_qrcode_base64')->where($map)->update($update);
        }else{
            $update['driver_id']=$driverId;
            Db::name('driver_qrcode_base64')->insertGetId($update);
        }
        $res[0]=$result;
        $res[1]=$result1;
        return $res;
    }
    public static function makeQRCode($value,$path,$file_name,$errorCorrectionLevel='L',$matrixPointSize = 6,$uid=''){
        if(empty($value) || empty($path) || empty($file_name)){
            return array('status'=>0,'msg'=>'参数异常');
        }
        $pathss=$path;

        QRcode::png($value, $path, $errorCorrectionLevel, $matrixPointSize, 2);

        $logo = ROOT_PATH.'/public/log.png';//logo图片
        if ($logo !== FALSE) {
            $path = imagecreatefromstring(file_get_contents($path));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($path);//二维码图片宽度
            $QR_height = imagesy($path);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 5; //logo图片在二维码图片中宽度大小
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale; //logo图片在二维码图片中高度大小
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($path, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                $logo_qr_height, $logo_width, $logo_height);
        }
        //输出图片
       /* Header("Content-type: image/png");
        ImagePng($path);*/
        ImagePng($path,$pathss);

        $QR['qr_code']['name'] = array_pop(explode('/',$pathss));
        $QR['qr_code']['tmp_name'] = $pathss;
        $ftp = new FTP('','','','','WEB');
        $s = $ftp->ftpUploads($QR,$file_name,$uid);
        unlink($pathss);
        if(!$s['status']){
            return $s;
        }
        return array('status'=>1,'qr_code'=>$s['data']['qr_code']);
    }

    public function test()
    {
        $result=$this->produce_code(101,1);
        //$data='image/png;base64,'.base64_encode($result[0]);
        echo '<img src="'.$result.'">';
        exit;

    }
}
